﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;

namespace TMDT.Web.UI.Controls
{
    public partial class DanhSachSanPham : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            var service = new SanPhamManager();
            dtlSanPham.DataSource = service.GetSanPham();
            //PagedDataSource dts = new PagedDataSource();
            //dts.DataSource = service.GetSanPham();
            //dts.AllowPaging = true;
            //dts.PageSize = 2;
            //dts.CurrentPageIndex = 1;
            //dtlSanPham.DataSource = dts;
            dtlSanPham.DataBind();
        }

        protected void dtlSanPham_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            var hinhAnh = new HinhAnhManager();
            BusinessObjects.SanPham sanPham = (BusinessObjects.SanPham) e.Item.DataItem;

            var imgHinhAnh = e.Item.FindControl("imgHinhAnh") as Image;
            imgHinhAnh.ImageUrl = hinhAnh.GetHinhAnhByIDSanPham(sanPham.IDSanPham).URL;
        }
    }
}